package com.demo.demo3;

import com.demo.demo3.utils.TreeUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Demo3 {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class DataVO {
        private String colA;
        private String colB;
        private String colC;

        public String getKey() {
            return colA + "#" + colB + "#" + colC;
        }

        public static String getParentKey(String key) {
            if (key == null || key.isEmpty()) {
                return null;
            }
            String[] parts = key.split("#");
            if (parts.length == 1) {
                return null;
            }
            return String.join(
                    "#",
                    Arrays
                            .asList(parts)
                            .subList(0, parts.length - 1)
            );
        }
    }

    public static void main(String[] args) {
        List<DataVO> list = new ArrayList<>();
        list.add(new DataVO("AAA", "BBB", "CCC"));
        list.add(new DataVO("AAA", "BBB", "DDD"));
        list.add(new DataVO("AAA", "BBB", "EEE"));
        list.add(new DataVO("FFF", "HHH", "KKK"));
        list.add(new DataVO("FFF", "HHH", "SSS"));
        list.add(new DataVO("FFF", "TTT", "SSS"));

        List<TreeUtil.TreeVO<DataVO, String>> trees = TreeUtil.listToTree(
                list,
                DataVO::getKey,
                DataVO::getParentKey
        );

        System.out.println(trees);
    }

}
